package com.hua.designpattern.structural.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author joshua
 * 
 *         <pre>
 * 
 * 过滤器模式（Filter Pattern）是一种设计模式，这种模式允许开发人员使用不同的标准来过滤一组对象，通过逻辑运算以解耦的方式把它们连接起来。
 * 这种类型的设计模式属于结构型模式，它结合多个标准来获得单一标准。
 * 
 *         </pre>
 *
 */
public class TestFilter {

	public static void main(String[] args) {
		List<BaseObject> objects = new ArrayList<BaseObject>();
		objects.add(new BaseObject("baseObject1", true, 100));
		objects.add(new BaseObject("baseObject2", true, 110));
		objects.add(new BaseObject("baseObject3", true, 120));
		objects.add(new BaseObject("baseObject4", false, 130));
		objects.add(new BaseObject("baseObject5", false, 150));
		objects.add(new BaseObject("baseObject5", false, 150));
		objects.add(new BaseObject("bo6", true, 110));
		objects.add(new BaseObject("bo7", true, 120));
		objects.add(new BaseObject("bo8", true, 130));
		objects.add(new BaseObject("bo9", false, 140));
		objects.add(new BaseObject("bo10", false, 150));
		objects.add(new BaseObject("bo10", false, 150));

		display(new FilterName("base").matchFilter(objects), "contain base");
		display(new FilterNameNot("Object").matchFilter(objects), "not contain Object");
		display(new FilterSingle().matchFilter(objects), "single");
		display(new FilterSingleNot().matchFilter(objects), "not single");
		display(new FilterSize(140).matchFilter(objects), "more than 140");
		display(new FilterAdd(new FilterName("base"), new FilterName("5")).matchFilter(objects), "contain base and 5");
		display(new FilterAdd(new FilterName("base"), new FilterSingle()).matchFilter(objects),
				"contain base and single");
		display(new FilterOr(new FilterName("base"), new FilterName("5")).matchFilter(objects), "contain base or 5");
		display(new FilterOr(new FilterName("base"), new FilterSize(140)).matchFilter(objects),
				"contain base or more than 140");
	}

	private static void display(List<BaseObject> objects, String msg) {
		if (objects == null || objects.size() == 0) {
			System.err.println("Empty output");
		} else {
			System.out.println(msg + "--------total num------" + objects.size());
			for (BaseObject object : objects) {
				System.out.println(object.toString() + "[name:" + object.getName() + " single:" + object.isSingle()
						+ " size:" + object.getSize() + "]");
			}
		}
	}

}
